<template>
  <div id="aCoursesList" class="bg-fa of">
    <!-- /课程列表 开始 -->
    <section class="container">
      <header class="comm-title">
        <h2 class="fl tac">
          <span class="c-333">全部课程</span>
        </h2>
      </header>
      <section class="c-sort-box">
        <section class="c-s-dl">
          <dl>
            <dt>
              <span class="c-999 fsize14">课程类别</span>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">
                <li>
                  <a
                    title="全部"
                    href="#"
                    @click="searchAll()"
                    :class="{ active: oneIndex == -1 }"
                    >全部</a
                  >
                </li>
                <li
                  v-for="(item, index) in subjectNestedList"
                  :key="index"
                  @click="changeTwoSubject(item.id, item.children, index)"
                  :class="{ active: oneIndex == index }"
                >
                  <a :title="item.title" href="#">{{ item.title }}</a>
                </li>
              </ul>
            </dd>
          </dl>
          <dl>
            <dt>
              <span class="c-999 fsize14"></span>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">
                <li
                  v-for="(item, index) in subSubjectList"
                  :key="index"
                  @click="searchTwoSubject(item.id, index)"
                  :class="{ active: twoIndex == index }"
                >
                  <a :title="item.title" href="#">{{ item.title }}</a>
                </li>
              </ul>
            </dd>
          </dl>
          <div class="clear"></div>
        </section>
        <div class="js-wrap">
          <section class="fr">
            <span class="c-ccc">
              <i class="c-master f-fM">1</i>/
              <i class="c-666 f-fM">1</i>
            </span>
          </section>
          <section class="fl">
            <ol class="js-tap clearfix">
              <li>
                <a
                  title="销量"
                  href="#"
                  :class="{ 'current bg-orange': searchObj.buyCountSort == 1 }"
                  @click="buyCountSort()"
                  >高销量&nbsp;
                  <!-- <span :class="{hide:searchObj.gmtCreateSort!=''}">↓</span> -->
                </a>
              </li>
              <li>
                <a
                  title="最新"
                  href="#"
                  :class="{ 'current bg-orange': searchObj.gmtCreateSort == 1 }"
                  @click="gmtCreateSort()"
                  >最新</a
                >
              </li>
              <li>
                <a
                  title="价格"
                  href="#"
                  :class="{
                    'current bg-orange':
                      searchObj.priceSort == 1 || searchObj.priceSort == 2,
                  }"
                  @click="priceSort()"
                  >价格&nbsp;
                  <span>{{ pricePoint }}</span>
                </a>
              </li>
            </ol>
          </section>
        </div>
        <div class="mt40">
          <!-- /无数据提示 开始-->
          <section class="no-data-wrap" v-if="data.total == 0">
            <em class="icon30 no-data-ico">&nbsp;</em>
            <span class="c-666 fsize14 ml10 vam"
              >没有相关数据，小编正在努力整理中...</span
            >
          </section>
          <!-- /无数据提示 结束-->
          <article class="comm-course-list" v-if="data.total > 0">
            <ul class="of" id="bna">
              <li v-for="item in data.items" :key="item.id">
                <div class="cc-l-wrap">
                  <section class="course-img">
                    <img
                      :src="item.cover"
                      class="img-responsive"
                      :alt="item.title"
                    />
                    <div class="cc-mask">
                      <a
                        :href="'/course/' + item.id"
                        title="开始学习"
                        class="comm-btn c-btn-1"
                        >开始学习</a
                      >
                    </div>
                  </section>
                  <h3 class="hLh30 txtOf mt10">
                    <a
                      :href="'/course/' + item.id"
                      :title="item.title"
                      class="course-title fsize18 c-333"
                      >{{ item.title }}</a
                    >
                  </h3>
                  <section class="mt10 hLh20 of">
                    <span class="fr jgTag bg-green">
                      <!-- 如果课程价格是0,则显示免费 -->
                      <i
                        class="c-fff fsize12 f-fA"
                        v-if="Number(item.price) === 0"
                        >免费</i
                      >
                      <!-- 如果课程价格不等于0,则显示价格 -->
                      <i
                        class="c-fff fsize12 f-fA"
                        v-if="Number(item.price) != 0"
                        >￥{{ item.price }}</i
                      >
                    </span>
                    <!-- <span class="fr jgTag bg-green">
                     
                    </span> -->
                    <span class="fl jgAttr c-ccc f-fA">
                      <i class="c-999 f-fA">{{ item.buyCount }}人购买</i>
                      |
                      <i class="c-999 f-fA">{{ item.viewCount }}人浏览</i>
                    </span>
                  </section>
                </div>
              </li>
            </ul>
            <div class="clear"></div>
          </article>
        </div>
        <!-- 公共分页 开始 -->
        <div>
          <div class="paging">
            <!-- undisable这个class是否存在，取决于数据属性hasPrevious -->
            <a
              :class="{ undisable: !data.hasPrevious }"
              href="#"
              title="首页"
              @click.prevent="gotoPage(1)"
              >首</a
            >
            <a
              :class="{ undisable: !data.hasPrevious }"
              href="#"
              title="前一页"
              @click.prevent="gotoPage(data.current - 1)"
              >&lt;</a
            >
            <a
              v-for="page in data.pages"
              :key="page"
              :class="{
                current: data.current == page,
                undisable: data.current == page,
              }"
              :title="'第' + page + '页'"
              href="#"
              @click.prevent="gotoPage(page)"
              >{{ page }}</a
            >
            <a
              :class="{ undisable: !data.hasNext }"
              href="#"
              title="后一页"
              @click.prevent="gotoPage(data.current + 1)"
              >&gt;</a
            >
            <a
              :class="{ undisable: !data.hasNext }"
              href="#"
              title="末页"
              @click.prevent="gotoPage(data.pages)"
              >末</a
            >
            <div class="clear" />
          </div>
        </div>
        <!-- 公共分页 结束 -->
      </section>
    </section>
    <!-- /课程列表 结束 -->
  </div>
</template>
<script>
import course from "@/api/course";
export default {
  data() {
    return {
      page: 1,
      data: {},
      subjectNestedList: [], // 一级分类列表
      subSubjectList: [], // 二级分类列表
      searchObj: {}, // 查询表单对象
      oneIndex: -1,
      twoIndex: -1,
      pricePoint: "", //价格升降序箭头
      courseId: ""
      // buyCountSort: "",
      // gmtCreateSort: "",
      // priceSort: "",
    };
  },
  created() {
    this.initCourseFirst();
    this.initSubject();
  },
  methods: {
    //首次进入课程分页查询第一页数据
    initCourseFirst() {
      course.getPageList(1, 8, this.searchObj).then((response) => {
        //得到后端传的map集合
        this.data = response.data.data;
      });
    },
    //获取一级分类
    initSubject() {
      course.getNestedTreeList().then((response) => {
        //拿到一级分类对象(包含了二级分类)
        this.subjectNestedList = response.data.data.list;
      });
    },
    //分页切换:点击其它页数时获取数据
    gotoPage(page) {
      course.getPageList(page, 8, this.searchObj).then((response) => {
        //得到后端传的map集合
        this.data = response.data.data;
      });
    },
    //点击一级分类显示对应的二级分类,并且查询出一级分类的课程
    changeTwoSubject(oneSubjectId, children, index) {
      //传入index是为了配合css选中效果 与:class="{active:oneIndex==index}"配合
      this.oneIndex = index;
      //点击一级分类需要清空选中的二级分类条件
      this.searchObj.subjectId = "";
      //点击一级分类需要清空选中的二级分类样式
      this.twoIndex = -1;
      this.searchObj.subjectParentId = oneSubjectId;
      this.gotoPage(1);

      this.subSubjectList = children;
    },
    //查找全部
    searchAll() {
      //如果oneIndex是-1,就会选中"全部"按钮,因此设置为-1
      //与:class="{active:oneIndex==-1}"相配合
      this.oneIndex = -1;
      this.twoIndex = -1;
      //清除二级分类列表的显示
      this.subSubjectList = [];
      //清除所有查询条件
      this.searchObj = {};
      //查询首页数据
      this.gotoPage(1);
    },
    //查找二级分类数据
    searchTwoSubject(twoSubjectId, index) {
      //设置条件查询的二级分类id
      this.searchObj.subjectId = twoSubjectId;
      //设置点击时选中样式,配合:class="{ active: twoIndex == index }"
      this.twoIndex = index;
      //重新查询首页数据
      this.gotoPage(1);
    },
    //根据销量降序排序
    buyCountSort() {
      //后端判断有值则进行排序
      this.searchObj.buyCountSort = 1;
      //清空其它的排序条件
      this.searchObj.gmtCreateSort = "";
      this.searchObj.priceSort = "";
      //清除价格的箭头
      this.pricePoint = "";
      //分页查询数据
      this.gotoPage(1);
    },
    //根据时间降序排序
    gmtCreateSort() {
      //后端判断有值则进行排序
      this.searchObj.gmtCreateSort = 1;
      //清空其它的排序条件
      this.searchObj.buyCountSort = "";
      this.searchObj.priceSort = "";
      //清除价格的箭头
      this.pricePoint = "";
      //分页查询数据
      this.gotoPage(1);
    },
    //根据价格升/降序排序
    priceSort() {
      //如果是首次点击或者原本是升序,则改成降序,并且清空其它的排序条件
      // this.searchObj.priceSort =1表示降序, =2 表示升序(后端判断)
      if (this.pricePoint == "" || this.pricePoint == "↑") {
        this.pricePoint = "↓";
        this.searchObj.priceSort = 1;
        this.searchObj.buyCountSort = "";
        this.searchObj.gmtCreateSort = "";
        this.gotoPage(1);
        //如果此时是降序,则改成升序,并且清空其它的排序条件
      } else if (this.pricePoint == "↓") {
        this.pricePoint = "↑";
        this.searchObj.priceSort = 2;
        this.searchObj.buyCountSort = "";
        this.searchObj.gmtCreateSort = "";
        this.gotoPage(1);
      }
    },
  },
};
</script>
<style scoped>
.active {
  background: #bdbdbd;
}
.hide {
  display: none;
}
.show {
  display: block;
}
</style>